package com.apobates.jforum2.troll.threads.biz.impl.api;

import com.apobates.jforum2.troll.regular.ActionOperatorLog;
import com.apobates.jforum2.troll.regular.ActionOperatorLogProvider;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.regular.event.TopicActionLog;
import com.apobates.jforum2.troll.regular.event.TopicMoveLog;
import com.apobates.jforum2.troll.threads.entity.Topic;
import com.apobates.jforum2.troll.threads.mapper.ThreadsVoMapperEntity;
import com.apobates.jforum2.troll.utils.core.api.ActionEventCulpritor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.function.BiConsumer;

/**
 * SpringCloudStream RabbitMQ操作日志处理接口
 * 谁负责谁声明.若将StreamBridge提到regular模块中该如何声明呢？
 */
@Component
public class TopicActionLogStreamProvider implements ActionOperatorLogProvider {
    @Autowired
    private StreamBridge streamBridge;
    private final static Logger logger = LoggerFactory.getLogger(TopicActionLogStreamProvider.class);

    @Override
    public void send(ActionOperatorLog log) {
        logger.error("[LOG][Threads]start send");
        streamBridge.send(log.getBindingNames()+"-out-0", log.getPayload());
        logger.error("[LOG][Threads]send finish");
    }

    public void action(final ForumActionEnum action, Topic topic, final ActionEventCulpritor culpritor){
        TopicActionLog ta = new TopicActionLog();
        ta.setMemberId(culpritor.getMemberId());
        ta.setMemberNickname(culpritor.getMemberNickname());
        ta.setAction(action);
        ta.setTopicId(topic.getId());
        ta.setTopicTitle(topic.getTitle());
        ta.setRodeMember(culpritor.getMemberId());
        ta.setIpAddr(culpritor.getIpAddr());
        ta.setToken(culpritor.getToken());
        String topicLink = ThreadsVoMapperEntity.produceLink.apply(topic.getId(), topic.getBoardId(), topic.getVolumesId());
        ta.setLink(topicLink);
        ta.setDateTime(LocalDateTime.now());
        this.send(ta);
    }

    public BiConsumer<Topic, ActionEventCulpritor> publishTopic(final ForumActionEnum action){
        return (t, culpritor)->{
            TopicActionLog ta = new TopicActionLog();
            ta.setMemberId(culpritor.getMemberId());
            ta.setMemberNickname(culpritor.getMemberNickname());
            ta.setAction(action);
            ta.setTopicId(t.getId());
            ta.setTopicTitle(t.getTitle());
            ta.setRodeMember(culpritor.getMemberId());
            ta.setIpAddr(culpritor.getIpAddr());
            ta.setToken(culpritor.getToken());
            String topicLink = ThreadsVoMapperEntity.produceLink.apply(t.getId(), t.getBoardId(), t.getVolumesId());
            ta.setLink(topicLink);
            ta.setDateTime(LocalDateTime.now());
            this.send(ta);
        };
    }

    public void moveTopic(final Topic topic, final long topicNewBoard, final long manager, final String managerNickname){
        TopicMoveLog tm = new TopicMoveLog();
        String topicLink = ThreadsVoMapperEntity.produceLink.apply(topic.getId(), topic.getBoardId(), topic.getVolumesId());
        tm.setId(topic.getId());
        tm.setTitle(topic.getTitle());
        tm.setMemberId(topic.getMemberId());
        tm.setMemberNickname(topic.getMemberNickname());
        tm.setBoard(topicNewBoard);
        tm.setLink(topicLink);
        tm.setManagerNickname(managerNickname);
        tm.setManager(manager);
        tm.setDateTime(LocalDateTime.now());
        this.send(tm);
    }
}
